package com.md.drcs.dicom.sql;

import com.md.common.utils.date.DateUtil;
import com.md.drcs.dicom.req.PatientStudyQueryReq;
import com.md.drcs.dicom.req.StudyCollectGroupReq;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.LocalTime;

public class StudySQLBuiler {

    public String findPatientStudy(PatientStudyQueryReq req) {
        StringBuilder sql = new StringBuilder();
        sql.append("select ");
        sql.append("p.nPatId, p.sHospPatId, p.sHospId, p.sName, ");
        sql.append("p.sEName, p.sSex, p.sPatIdInPacs, ");
        sql.append("p.sPatIdinFile, p.sPatNameInFile, p.sImagePath, ");
        sql.append("p.sPhoneTel, p.sAddress, p.sPostCode, ");
        sql.append("s.nStuId, s.sStuIdInPacs, s.sStuAge, s.stuDate, s.sStuPart, ");
        sql.append("s.sRequDept, s.sHosNo, s.sBedNo, s.sMod, s.sStuType ");
        sql.append("from t_study s ");
        sql.append("left join t_patient p on (s.nPatId = p.nPatID) ");
        sql.append("where 1 = 1");
        if (req.getBegin() != null) {
            req.setBeginDate(LocalDateTime.of(DateUtil.parseLocalDate(req.getBegin()), LocalTime.MIN));
            sql.append(" and s.stuDate >= #{beginDate}");
        }
        if (req.getEnd() != null) {
            req.setEndDate(LocalDateTime.of(DateUtil.parseLocalDate(req.getEnd()), LocalTime.MAX));
            sql.append(" and s.stuDate <= #{endDate}");
        }
        if (StringUtils.isNotBlank(req.getHospId()))
            sql.append(" and s.sHospId = #{hospId}");
        if (StringUtils.isNotBlank(req.getsName()))
            sql.append(" and p.sName LIKE CONCAT(CONCAT('%', #{sName}), '%')");
        if (StringUtils.isNotBlank(req.getsPatIdInPacs()))
            sql.append(" and s.sPatIdInPacs LIKE CONCAT(CONCAT('%', #{sPatIdInPacs}), '%')");
        if (StringUtils.isNotBlank(req.getsMod()))
            sql.append(" and s.sMod = #{sMod}");
        sql.append(" order by s.nStuid desc");
        return sql.toString();
    }

    /*

        select
         s.nStuId, s.nPatId, s.sStuAge, s.sMod, s.sRequDept, s.sRequDoc,
         s.stuDate, p.sSex, p.sName, s.sStuType
        from t_study s
        left join t_patient p on (s.nPatId = p.nPatID)
        where s.StuDate between #{beginDateTime} and #{endDateTime}
        and s.sMod in ('CT', 'MR') and s.bCloseCloud = 0

     */

    public String getStudyCollectGroup(StudyCollectGroupReq req) {
        StringBuilder sql = new StringBuilder();
        sql.append("select ");
        sql.append(" s.nStuId, s.nPatId, s.sStuAge, s.sMod, s.sRequDept, s.sRequDoc,");
        sql.append(" s.stuDate, p.sSex, p.sName, s.sStuType, s.sStuPart");
        sql.append(" from t_study s");
        sql.append(" left join t_patient p on (s.nPatId = p.nPatID)");
        sql.append(" where s.StuDate between #{beginDateTime} and #{endDateTime}");
        // 检查方式：0 - 全部，1 - 放射，2 - 超声，3 - 检验，4 - 内镜，5 - 病理，6 - 心电图，7 - 牙片
        if (req.getMod() == 0) {
        } else if (req.getMod() == 1) {
            sql.append(" and s.sMod not in ('ES','US','ECG','JY','PX','BL') and (s.bCloseCloud = 0 or s.bCloseCloud is NULL)");
        } else if (req.getMod() == 2) {
            sql.append(" and s.sMod in ('US')");
        } else if (req.getMod() == 3) {
            sql.append(" and s.sMod in ('JY')");
        } else if (req.getMod() == 4) {
            sql.append(" and s.sMod in ('ES')");
        } else if (req.getMod() == 5) {
            sql.append(" and s.sMod in ('BL')");
        } else if (req.getMod() == 6) {
            sql.append(" and s.sMod in ('ECG')");
        } else if (req.getMod() == 7) {
            sql.append(" and s.sMod in ('PX')");
        } else {
            sql.append(" and 1 = 2");
        }

        if (req.getMod() == 2) {
            if (req.getIsHosp() == null) {
            } else if (req.getIsHosp() == 0) {
                sql.append(" and s.sStuType in ('门诊')");
            } else if (req.getIsHosp() == 1) {
                sql.append(" and s.sStuType not in ('门诊')");
            }
        } else if (req.getMod() != 3) {
            if (req.getIsHosp() == null) {

            } else if (req.getIsHosp() == 0) {
                sql.append(" and s.sStuType in ('门诊')");
            } else if (req.getIsHosp() == 1) {
                sql.append(" and s.sStuType not in ('门诊')");
            }
        }
        sql.append(" order by s.nStuId desc ");
        return sql.toString();
    }

}
